//
//  HMDiscoverSearchView.swift
//  GuoGuo
//
//  Created by 艾小新 on 16/7/29.
//  Copyright © 2016年 xiaoxin. All rights reserved.
//

import UIKit

class HMDiscoverSearchView: UIView {

    @IBOutlet weak var searchTF: UITextField!
    @IBOutlet weak var cancelSearch: UIButton!
 
    // 修改View在这里，因为init里面拿不到UITexrField等值，还未初始化
    override func awakeFromNib() {
        
        searchTF.leftView = leftView
        searchTF.leftView?.frame = CGRectMake(0, 0, self.frame.height, self.frame.height)
        searchTF.leftViewMode = .Always
        
        // 修改搜索框的边框
//        searchTF.layer.borderWidth = 2
//        searchTF.layer.borderColor = UIColor.yellowColor().CGColor
//        searchTF.layer.cornerRadius = 15
//        searchTF.layer.masksToBounds = true
        
    }
    
    
    // 如果View是xib加载的，那应该写一个类方法，让对方号调用
    
    class func loadSearchView() -> HMDiscoverSearchView {
        
        return NSBundle.mainBundle().loadNibNamed("HMDiscoverSearchView", owner: nil, options: nil).last! as! HMDiscoverSearchView

    }
    
    
    
    @IBOutlet weak var searchTFTrailingCon: NSLayoutConstraint!
    // 触发搜索框成为第一响应者的时候
    @IBAction func secrchDidBegin(sender: AnyObject) {
        
        searchTFTrailingCon.constant = cancelSearch.frame.width
        
        UIView.animateWithDuration(0.25) { 
            self.searchTF.layoutIfNeeded()
        }
        
        
    }
    
    
    // 取消时候-
    @IBAction func cancelSearchClick(sender: AnyObject) {
        
        searchTF.resignFirstResponder()
        
        searchTFTrailingCon.constant = 0
        
        UIView.animateWithDuration(0.25) {
            self.searchTF.layoutIfNeeded()
        }
    }
    
    
    
    // MARK: - 懒加载控件
    private lazy var leftView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        // 设置 内容模式
        view.contentMode = .Center
        return view
    }()

    
}
